import java.util.Scanner;

public class AAA {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个正整数:");
        int number = scanner.nextInt();

        String chineseNumber = convertToChinese(number);
        System.out.println("汉语读法: " + chineseNumber);
        scanner.close();
    }

    public static String convertToChinese(int number) {
        if (number == 0) {
            return "零";
        }

        String[] units = {"", "十", "百", "千"};
        String[] digits = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        StringBuilder chinese = new StringBuilder();

        if (number < 0) {
            chinese.append("负");
            number = -number;
        }

        int groupSize = 4;
        int group = 1;

        while (number > 0) {
            int part = number % (int) Math.pow(10, groupSize);
            if (part > 0) {
                chinese.insert(0, partToChinese(part, units) + " ");
            }
            number /= (int) Math.pow(10, groupSize);
            if (number > 0) {
                group++;
            }
        }

        // 处理末尾的零
        if (chinese.indexOf("零千") != -1) {
            chinese.replace(chinese.lastIndexOf("零千"), chinese.lastIndexOf("零千") + 2, "");
        }

        // 处理连续的零
        chinese = new StringBuilder(chinese.toString().replace("零零", "零"));

        return chinese.toString();
    }

    private static String partToChinese(int part, String[] units) {
        StringBuilder partChinese = new StringBuilder();
        int rank = 0;

        while (part > 0) {
            int digit = part % 10;
            if (digit > 0) {
                partChinese.insert(0, digits[digit] + units[rank]);
            } else if (partChinese.length() > 0 && partChinese.charAt(0) != '零') {
                partChinese.insert(0, "零");
            }
            part /= 10;
            rank++;
        }
        return partChinese.toString();
    }

    private static String[] digits = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
}
